/**
 * Created by abdulazizalonizi on 8/9/17.
 */
import java.io.*;
import java.util.Scanner ;
public class FruitDriver {
    public static void main (String [] args)
    {

        Fruit[] fruitsArr = read_fruitsObjects(); // this method will read from file instead of initializing array fruit

        int searchResult = searchForFruit("coco"); //this method will search for fruit by name

        if(searchResult >-1)
            System.out.println("fruit was found !");
        else
            System.out.println("fruit was not found !");


    }

    public static int lineCounter()
    {
        try {
            int Lines_Counter = 0;

            FileInputStream FIS = new FileInputStream("fruits.txt");
            Scanner kb = new Scanner(FIS);

            while (kb.hasNextLine()) {
                kb.nextLine();
                Lines_Counter++;

            }

             kb.close();
             return Lines_Counter;

        }
        catch(Exception me)
        {
            System.out.println("Exception Found !");
            return 0 ;
        }

    }

    public static Fruit[] read_fruitsObjects ()
    {
        try {

            FileInputStream FIS = new FileInputStream("fruits.txt");
            Scanner kb = new Scanner(FIS);



            if(lineCounter()==0)
                return null;


            Fruit[] fruits_Array = new Fruit[lineCounter()];


            for (int i = 0; i < fruits_Array.length; i++) {
                fruits_Array[i] = new Fruit( kb.next(), kb.nextDouble(), kb.nextDouble() );

            }
            kb.close();

            return fruits_Array;

        }
        catch(Exception me)
        {
            System.out.println("Exception Found !");
            return null ;
        }

    }

    public static void display_all_fruitsInfo ()
    {
        Fruit[] fruitsArr = read_fruitsObjects();

        if (fruitsArr==null)
        {
            System.out.println("no Fruit Array found !!");
        }

        System.out.print("Name          Stock Quantity(KG)          Price per KG (SR)\n");

        for(int i = 0 ; i <fruitsArr.length ; i++)
        {

            System.out.print(fruitsArr[i].getName()+"                   "+fruitsArr[i].getStockQuantity()+"                     "+fruitsArr[i].getPricePerKilogram()+"\n");

        }

    }

    public static int searchForFruit (String fruit_name ) //return index of the fruit
    {
        Fruit[] otherFruitArr = read_fruitsObjects();

        for(int i = 0 ; i <otherFruitArr.length ; i++)
        {
            if (fruit_name.equalsIgnoreCase(otherFruitArr[i].getName()))
                return i ;


        }
        return -1 ; //-1 for fruit not found
    }

    public static void display_fruitInfo(String name)
    {
        Fruit[] fruitsArr = read_fruitsObjects();

        int index = searchForFruit(name);

        if (index == -1)
        {
            System.out.println("Fruit was not found !!");
        }
        else if (fruitsArr==null)
        {
            System.out.println("No Fruit Array Found !");
        }
        else
        {
            System.out.print("Name          Stock Quantity(KG)          Price per KG (SR)\n");
            System.out.println(fruitsArr[index].getName()+"             "+fruitsArr[index].getStockQuantity()+"                          "+fruitsArr[index].getPricePerKilogram());
        }
    }

    public static void overwrite_TXT(Fruit[] FruitsArr)
    {
        try {
            FileOutputStream FOS = new FileOutputStream("fruits.txt");
            PrintWriter pw = new PrintWriter(FOS);

            for (int i = 0; i < FruitsArr.length; i++) {
                if(i==FruitsArr.length-1)
                    pw.print(FruitsArr[i]);
                else
                pw.println(FruitsArr[i]);

            }
            pw.close();
            FOS.close();
        }catch(Exception me)
        {
            System.out.println("Exception Found !");
        }

    }

    public static void modify_Stock(String fruitName , double stockQuantity)
    {
        Fruit[] arrFruits = read_fruitsObjects();

      int index = searchForFruit(fruitName);

      if(index==-1) //fruit not found by method searchForFruit
      {
          System.out.println("Fruit Was not Found !");
      }
      else if (arrFruits==null)
      {
          System.out.println("No Fruit Array Found !");
      }
      else
      {
          arrFruits[index].setStockQuantity(stockQuantity);
          overwrite_TXT(arrFruits);
      }

    }

    public static void modify_Price(String fruitName , double PricePerKilo)
    {
        Fruit[] arrFruits = read_fruitsObjects();

        int index = searchForFruit(fruitName);

        if(index==-1) //fruit not found by method searchForFruit
        {
            System.out.println("Fruit Was not Found !");
        }
        else if (arrFruits==null)
        {
            System.out.println("No Fruit Array Found !");
        }
        else
        {
            arrFruits[index].setPricePerKilogram(PricePerKilo);
            overwrite_TXT(arrFruits);
        }

    }

    public static void add_Fruit(String name , double stockQuantity , double priceKilo) //needs search method!
    {

        int index = searchForFruit(name);

        if (index ==-1) {
            Fruit[] FruitsArr = read_fruitsObjects();
            Fruit[] FruitsArr_ADD = new Fruit[(read_fruitsObjects().length) + 1];

            for (int i = 0; i < FruitsArr.length; i++) {
                FruitsArr_ADD[i] = FruitsArr[i];
            }

            FruitsArr_ADD[FruitsArr_ADD.length - 1] = new Fruit(name, stockQuantity, priceKilo);
            overwrite_TXT(FruitsArr_ADD);
        }
        else
            System.out.println("Fruit already exist !");






    }

    public static void remove_Fruit(String name) //needs search method !
    {

       int index =  searchForFruit(name);

       if(index !=-1) {
           Fruit[] FruitsArr = read_fruitsObjects();
           Fruit[] FruitsArr_DEL = new Fruit[FruitsArr.length - 1];

           for (int i = 0; i < index; i++) {

               FruitsArr_DEL[i] = FruitsArr[i];

           }

           for (int i = index + 1; i < FruitsArr.length; i++) {
               FruitsArr_DEL[i - 1] = FruitsArr[i];
           }

           overwrite_TXT(FruitsArr_DEL);
       }
       else
       {
           System.out.println("Fruit Was not Found !!");
       }


    }

    public static void sell_Fruit () {
          try {
              FileOutputStream FOS = new FileOutputStream("bills.txt", true);
              PrintWriter pw = new PrintWriter(FOS);

              Scanner kb = new Scanner(System.in);

              String name = null;
              double quantity = 0.0;

              Fruit[] fruitsArr = read_fruitsObjects();
              char choice = 'Y';
              double priceCounter = 0.0;

              //int [] Fruits_Sold_indices = new int[fruitsArr.length];
              // double [] Quantities = new double[fruitsArr.length];
              //int i = 0;


              while (choice == 'Y') {


                  System.out.println("Enter Fruit Name and Quantity in KG");
                  name = kb.next();
                  quantity = kb.nextDouble();


                  int index = searchForFruit(name);

                  if (index == -1)
                      System.out.println("Fruit was not found !");
                  else {
                      if (quantity >= fruitsArr[index].getStockQuantity()) {
                          System.out.println("Insufficient stock. Stock available is " + fruitsArr[index].getStockQuantity() + " kilograms");
                      } else {
                          System.out.println("Fruit           Quantity(KG)           Price Per Kilo(SR)             Subtotal(SR)");
                          System.out.println(fruitsArr[index].getName() + "              " + quantity + "                       " + fruitsArr[index].getPricePerKilogram() + "                           " + fruitsArr[index].sellFruit(quantity));
                          pw.println("Fruit           Quantity(KG)           Price Per Kilo(SR)             Subtotal(SR)");
                          pw.println(fruitsArr[index].getName() + "              " + quantity + "                       " + fruitsArr[index].getPricePerKilogram() + "                           " + fruitsArr[index].sellFruit(quantity)+"\n");
                          // Fruits_Sold_indices[i]= index ;
                          //Quantities[i]=quantity;
                          priceCounter = priceCounter + fruitsArr[index].sellFruit(quantity);
                          fruitsArr[index].setStockQuantity(fruitsArr[index].getStockQuantity() - quantity);
                          overwrite_TXT(fruitsArr);


                      }
                  }

                  System.out.println("Do you want buy another fruit (Y/N): ");
                  choice = kb.next().charAt(0);
                  // i++;

              }

              System.out.println("Your Subtotal is " + priceCounter);
              pw.close();
              FOS.close();

          }
          catch(Exception me)
          {
              System.out.println("Exception i/o Found !");

          }

       // System.out.println("Fruit           Quantity(KG)           Price Per Kilo(SR)             Subtotal(SR)");
        //for (i =0 ; i <Fruits_Sold_indices.length ; i++)
        //{
        //    System.out.println(fruitsArr[Fruits_Sold_indices[i]].getName()+"         "+Quantities[i]+ "           "+fruitsArr[Fruits_Sold_indices[i]].getPricePerKilogram() +"         " +fruitsArr[Fruits_Sold_indices[i]].sellFruit(quantity));
        //}
       // System.out.println("Total                                                                   "+priceCounter);


    }






























}
